package com.zhongge.gui;

import com.zhongge.user.AdminUser;
import com.zhongge.user.NormalUser;
import com.zhongge.user.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 登录窗口
 */
public class LoginFrame extends JFrame {
    private JTextField nameField;
    private JRadioButton adminRadio;
    private JRadioButton userRadio;
    
    public LoginFrame() {
        setTitle("图书管理系统 - 登录");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null); // 居中显示
        setResizable(false);
        
        // 创建主面板
        JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        
        // 添加标题
        JLabel titleLabel = new JLabel("图书管理系统");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
        titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
        mainPanel.add(titleLabel, BorderLayout.NORTH);
        
        // 创建表单面板
        JPanel formPanel = new JPanel(new GridLayout(3, 1, 10, 10));
        
        // 用户名输入
        JPanel namePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JLabel nameLabel = new JLabel("用户名: ");
        nameField = new JTextField(15);
        namePanel.add(nameLabel);
        namePanel.add(nameField);
        
        // 用户类型选择
        JPanel typePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JLabel typeLabel = new JLabel("用户类型: ");
        adminRadio = new JRadioButton("管理员");
        userRadio = new JRadioButton("普通用户");
        ButtonGroup group = new ButtonGroup();
        group.add(adminRadio);
        group.add(userRadio);
        adminRadio.setSelected(true); // 默认选择管理员
        
        typePanel.add(typeLabel);
        typePanel.add(adminRadio);
        typePanel.add(userRadio);
        
        // 登录按钮
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JButton loginButton = new JButton("登录");
        loginButton.setPreferredSize(new Dimension(100, 30));
        loginButton.addActionListener(new LoginListener());
        buttonPanel.add(loginButton);
        
        // 添加到表单面板
        formPanel.add(namePanel);
        formPanel.add(typePanel);
        formPanel.add(buttonPanel);
        
        // 添加到主面板
        mainPanel.add(formPanel, BorderLayout.CENTER);
        
        // 设置内容面板
        setContentPane(mainPanel);
    }
    
    // 登录按钮监听器
    private class LoginListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String userName = nameField.getText().trim();
            if (userName.isEmpty()) {
                JOptionPane.showMessageDialog(LoginFrame.this, "请输入用户名", "提示", JOptionPane.WARNING_MESSAGE);
                return;
            }
            
            // 创建用户对象
            User user;
            if (adminRadio.isSelected()) {
                user = new AdminUser(userName);
            } else {
                user = new NormalUser(userName);
            }
            
            // 保存当前用户
            GUIMain.setCurrentUser(user);
            
            // 关闭登录窗口，打开主窗口
            dispose();
            new MainFrame().setVisible(true);
        }
    }
}